// pages/rent/rent.js

const app = getApp();

Page({
  data: {
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '乐巢公寓',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333",
    background: [
      {img: ""},
      {img: ""}
    ],
    list: [
      {id:0,path:'', title: "", image: ""},
    ]

  },
  onLoad(options) {

  },
  onReady() {

  },
  onShow() {
    this.getRoomList();
  },
  getRoomList(page = 0) {
    const v = this;
    let url = app.globalData.host + '/api/rent/room';
    let token = app.globalData.token;
    wx.request({ url: url, data: {token: token}, success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let d = r.data;
          let l = d.list || [];
          for (let i = 0, il = l.length; i < il; i++) {
            l[i].image = l[i].room_img || '';
            l[i].title = l[i].address || l[i].remark;
            
          }

          const newData = {
            list: l,
          }
          console.log("getRoomList ",l, r, newData);
          v.setData(newData);
        }
      }
    })
  },
  navigateTo(ev) {
    const v = this;
    const ds = ev.target.dataset;
    const idx = parseInt(ds.idx) || 0;
    console.log('navigateTo', idx, ev);
    if (idx > -1) {
      let id = 0;
      const info = v.data.list[idx];
      if (info) {
        id = info.room_id || 0;
        app.globalData.info = info;
      }
      wx.navigateTo({ url: "/pages/rent/applyRent?id=" + id});
    }
  },
  onHide() {

  },

  onUnload() {

  },

  onPullDownRefresh() {

  },
  onReachBottom() {

  },
  onShareAppMessage() {

  }
})